package cn.itcast.tanhua.controller;

import cn.itcast.mongo.Counts;
import cn.itcast.tanhua.interceptor.UserHolder;
import cn.itcast.tanhua.server.TanhuaService;
import cn.itcast.tanhua.server.UserInfoService;

import cn.itcast.tanhua.server.UserService;
import cn.itcast.utils.JwtUtils;
import cn.itcast.vo.UserInfoVo;
import io.jsonwebtoken.Claims;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;


/**
 * @author Administrator
 */
@RestController
@RequestMapping(path = "/users")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private TanhuaService tanhuaService;

    @Autowired
    private UserService userService;

    /**
     * 查询用户基础信息
     *
     * @param userID
     * @param token
     * @return
     */
    @GetMapping
    public ResponseEntity userInfo(Long userID, @RequestHeader("Authorization") String token) {
        //1. 校验token
        boolean verifyToken = JwtUtils.verifyToken(token);
        if(!verifyToken) {
            return ResponseEntity.status(401).body(null);
        }
        //2. 获取token中的用户信息
        Claims claims = JwtUtils.getClaims(token);
        String id = claims.get("id").toString();

        if (userID == null) {
            userID = Long.valueOf(id);
        }

        UserInfoVo userInfo = userInfoService.findById(userID);

        return ResponseEntity.ok(userInfo);
    }
    /**
    * 更新用户头像
    */
    @PostMapping("/header")
    public ResponseEntity header(MultipartFile headPhoto
            , @RequestHeader("Authorization") String token) throws IOException {

        boolean verifyToken = JwtUtils.verifyToken(token);
        if (!verifyToken){
            return ResponseEntity.status(401).body(null);
        }
        //2、向userinfo中设置用户id
        Claims claims = JwtUtils.getClaims(token);
        Integer id = (Integer) claims.get("id");
        //调用service
        userInfoService.updateHeader(headPhoto,Long.valueOf(id));
        return ResponseEntity.ok(null);
    }


    /**
     *改手机号- 1 发送短信验证码
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode(){
        //1. 获取手机号
        String phone =UserHolder.getMobile();
        //2. 发送验证码
        userService.sendCode2(phone);
        //3. 返回数据
        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机号 - 2 校验验证码
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map){
        //1 调用map集合获取请求参数
        String phone =UserHolder.getMobile();
        String code = (String) map.get("verificationCode");
        //2 调用userService完成用户登录
        boolean retMap = userService.Verification(phone, code);
        //3 构造返回
        return ResponseEntity.ok(retMap);
    }

    /**
     * 保存修改手机号
     * @param map
     * @return
     */
    @PostMapping("/phone")
    public ResponseEntity phone(@RequestBody Map map){
        String phone = (String) map.get("phone");

        userService.phoneTest(phone);

        return ResponseEntity.ok(null);
    }


}